package com.qen.fx.custom.impl;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.qen.App;
import com.qen.fx.ViewIndex;
import com.qen.fx.custom.CustomBusiness;
import com.qen.fx.factory.AllMenuCallback;
import com.qen.fx.factory.SideCallback;
import com.qen.fx.tools.MenuTools;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TitledPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

@Data
public class DefaultBusiness extends CustomBusiness {

    @FXML
    private ImageView businessIcon;
    @FXML
    private ImageView isLikeIcon;
    @FXML
    private VBox AllvBox;
    @FXML
    private ScrollPane AllPane;
    private static String SideMenuId = null;
    private AllMenuCallback allMenuCallback;

    public void setAllMenuCallback(AllMenuCallback allMenuCallback) {
        this.allMenuCallback = allMenuCallback;
    }

    public DefaultBusiness() {

        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(ViewIndex.DefaultBusinessFXML));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        DefaultBusiness controller = fxmlLoader.getController();
        controller.setPrefHeight(ViewIndex.sidePreferHeight);
        controller.setPrefWidth(ViewIndex.contentPreferWidth);

        AllPane.setPrefWidth(ViewIndex.contentPreferWidth);
        AllPane.setPrefHeight(ViewIndex.sidePreferHeight);
        AllvBox.getChildren().addAll(controller.initDefaultBusiness());
    }

    @Override
    public VBox init() {
        VBox vb = new VBox();
        vb.setPrefWidth(ViewIndex.contentPreferWidth);
        vb.setPrefHeight(ViewIndex.sidePreferHeight);
        return vb;
    }

    public List<HBox> initDefaultBusiness() {
        JSONArray menus = MenuTools.getMenus();
        int mLen = menus.size();
        Image nolike = new Image(App.class.getResourceAsStream("icon/nolike.png"));
        Image like = new Image(App.class.getResourceAsStream("icon/like.png"));
//        businessIcon.setImage(new Image(App.class.getResourceAsStream("icon/tokencreate.png")));
//        isLikeIcon.setImage(new Image(App.class.getResourceAsStream("icon/nolike.png")));
        List<JSONObject> list = new ArrayList<>();
        for (int i = 0; i < mLen; i++) {
            JSONObject menu = menus.getJSONObject(i);
            JSONArray children = menu.getJSONArray("children");
            int len = children.size();
            for (int j = 0; j < len; j++) {
                JSONObject child = children.getJSONObject(j);
                list.add(child);
            }
        }
        int all = list.size();
        int hei = (int) Math.ceil(all / ViewIndex.DefaultColumnNumber);
        List<VBox> arr = new ArrayList<>();

        List<HBox> hBoxs = new ArrayList<>();
        for (int i = 0; i < all; i++) {
            JSONObject child = list.get(i);
            ImageView icon = new ImageView(new Image(App.class.getResourceAsStream(child.getString("icon"))));
            icon.setFitHeight(35);
            icon.setFitWidth(35);
            icon.setPickOnBounds(true);
            icon.setPreserveRatio(true);
            icon.setTranslateX(10);
            icon.setTranslateY(15);

            int islike = child.getIntValue("like");
            ImageView likeIcon = new ImageView(islike == 1 ? like : nolike);
            likeIcon.setFitHeight(20);
            likeIcon.setFitWidth(20);
            likeIcon.setPickOnBounds(true);
            likeIcon.setPreserveRatio(true);
            likeIcon.setTranslateX(180);
            likeIcon.setTranslateY(20);

            HBox imgBox = new HBox(icon, likeIcon);
            imgBox.setPrefHeight(59);
            imgBox.setPrefWidth(186);

            Label title = new Label(child.getString("title"));
            title.setStyle("-fx-font-weight: bold;");
            title.setFont(new Font(15));
            title.setTranslateX(10);
            title.setTranslateY(5);

            Label content = new Label(child.getString("content"));
            content.setPrefHeight(62);
            content.setPrefWidth(236);
            content.setTextFill(Color.web("#5e5d5d"));
            content.setWrapText(true);
            content.setTranslateX(10);
            content.setFont(new Font(10));

            VBox obj = new VBox(imgBox, title, content);
            obj.setPrefHeight(140);
            // 加70
            obj.setPrefWidth(ViewIndex.DefaultColumnWidth);
            obj.setStyle("-fx-background-color: #FFF;");
            obj.setCursor(Cursor.HAND);
            obj.setOpaqueInsets(new Insets(10, 10, 0, 0));
            String id = child.getString("id");
            String action = child.getString("action");
            obj.setOnMouseClicked(event -> {
                System.out.println("点击了" + id);
                if (!id.equals(SideMenuId)) {
                    SideMenuId = id;
                }
                String titleText = child.getString("title");
                String contentText = child.getString("content");
                boolean isLike = child.getIntValue("like") == 1;

                if (allMenuCallback != null) {
                    allMenuCallback.seleteBusiness(titleText, contentText, isLike, action);
                }
            });
            HBox.setMargin(obj, new Insets(5, 5, 5, 5));

            if (i % ViewIndex.DefaultColumnNumber == 0 || i == all - 1) {
                HBox hBox = new HBox(arr.stream().toArray(Node[]::new));
                hBoxs.add(hBox);
                arr = new ArrayList<>();
            }
            arr.add(obj);
        }
        AllvBox.setPrefHeight(hei * 140 + 100);
        return hBoxs;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

}
